package com.wbs.controller;

import com.wbs.common.JsonData;
import com.wbs.dto.DeptLevelDto;
import com.wbs.param.DeptParam;
import com.wbs.service.SysDeptService;
import com.wbs.service.SysTreeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {

    @Resource
    SysDeptService sysDeptService;

    @Resource
    SysTreeService sysTreeService;

    // 进入页面的接口
    @RequestMapping("/dept.page")
    public ModelAndView page(){
        return new ModelAndView("dept");
    }

    // 新增接口
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveDept(DeptParam deptParam){
        sysDeptService.save(deptParam);
        return JsonData.success();
    }

    // 部门树
    @RequestMapping("/tree.json")
    @ResponseBody
    public JsonData tree(){
        List<DeptLevelDto> dtoDeptList=sysTreeService.deptTree();  // 得到部门树状列表
        return JsonData.success(dtoDeptList);
    }

    // 更新部门。注意：中间层的部门发生变化的时候，下边的也都要跟着变化
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData update(DeptParam param){
        sysDeptService.update(param);
        return JsonData.success();
    }

}
